def read_and_write_file(input, headercmd,headeropt):
  """Reads a file and writes it to a new file, with sections separated by double dashes and content separated by single dashes. If there is a space, take only the first_command part.

  Args:
    input_file: The path to the input file.
    output_file: The path to the output file.

  Returns:
    None.
  """
  cmdlist = []
  optlist = []
  with open(input, "r") as input_file:
    
    
   
    cmd = ''
    for line in input_file:
      line = line.lstrip()
      if line.startswith("--"):
        cmd = line[2:].split()[0].replace("-","_")
        cmdlist.append(cmd)
      elif line.startswith("-"):
        opt = line[1:].split()[0]
        opt = opt.replace("-","_")
        optlist.append((cmd,opt))



  with open(headercmd, "w") as output_file:
    first_command = 0
    for cmd in cmdlist: 
      output_file.write("int callback_" + cmd + "(char *args);\n")
    for cmd in cmdlist:    
      if first_command == 0:
        first_command = 1
        output_file. write( "parser_callback_function  command[] = \n{\n " )
      else :
        output_file.write("},\n")

      
      output_file.write("{\t.command=\"" + cmd + "\",\n")
      output_file.write("\t.func = &callback_" + cmd + ",\n")
      output_file.write("\t.opt = callback_option_" + cmd + ",\n")
      output_file.write("\t.n_opt = SIZEOFOPTION( callback_option_" + cmd + ")\n")
    output_file.write("")
    output_file.write("}\n")
    output_file.write("}\n")

          
  with open(input, "r") as input_file:
    cmdold = ' '
    with open(headeropt, "w") as output_file:
      for cmd,opt in optlist:

        if cmdold != cmd : 
          output_file.write("//" + cmd + "\n")
        cmdold = cmd
        output_file.write("int callback_" + cmd+"_"+ opt+ "(char *args);\n")
      first_command = 0
      cmd = ''
      for line in input_file:
        line = line.lstrip()
        if line.startswith("--"):
          if first_command == 0:
            first_command = 1
          else :
            output_file.write("};\n")
          
          cmd = line[2:].split()[0].replace("-","_")
          output_file. write( "//"+ cmd +"\n" )
          output_file. write( "parser_callback_option callback_option_"+ cmd +"[] = { " )
          output_file.write("\n")
        elif line.startswith("-"):
          output_file.write("{\n")
           

          opt = line[1:].split()[0]
          output_file.write("\t.command=\"" + opt + "\",\n")
          
          opt = opt.replace("-","_")
          opt = cmd + '_' +opt  
          output_file.write("\t.func = &callback_" + opt + ",\n")
          output_file.write("},\n")
        else:
          output_file.write("")
      output_file.write("};\n")
      

read_and_write_file("app/parser/autogen/description.txt", "app/parser/autogen/fun.h","app/parser/autogen/opt.h")

